import { _decorator, Component, Graphics, Color, Node, Camera, Vec3, instantiate, Prefab, AssetManager } from 'cc';
import { Grid, gridConfig } from './Grid';
import { GameManager } from '../../Managers/GameManager';

const { ccclass, property} = _decorator;

@ccclass('GridVisualizer')
export class GridVisualizer extends Component {

    public grid: Grid = null!;


    @property(Camera)
    mainCamera: Camera = null;

    @property(Prefab)
    prefab: Prefab = null;

    @property(Node)
    public endNode: Node = null;

    @property(Node)
    public startNode: Node = null;



    public InitGrid() {
        // 初始化网格
        this.grid = new Grid(gridConfig.width, gridConfig.height);

        // 填充障碍物（示例：在中心区域设置障碍）
        for (let x = 3; x < 20; x++) {
            for (let y = 10; y < 12; y++) {
                this.grid.setCell(x, y, false); // false 表示不可通行
                const obj = instantiate(this.prefab);
                obj.setParent(this.node);
                obj.worldPosition = this.grid.gridToWorld(x, y);
            }
        }
        for (let x = 6; x < 8; x++) {
            for (let y = 13; y < 16; y++) {
                this.grid.setCell(x, y, false); // false 表示不可通行
                const obj = instantiate(this.prefab);
                obj.setParent(this.node);
                obj.worldPosition = this.grid.gridToWorld(x, y);
            }
        }

        // 测试网格数据
        console.log("Is (25,25) walkable?", this.grid.isWalkable(12, 12)); // 应返回 false
        this.mainCamera?.camera.initGeometryRenderer();
        console.log(this.grid.worldToGrid(this.endNode.worldPosition).x + " " + this.grid.worldToGrid(this.endNode.worldPosition).y);

        //Astarmanager.getInstance().Init(this.grid);

    }

    update(deltaTime: number) {
        this.drawGrid();
        // if (this.gameManager.tanks[0] != null && this.gameManager.tanks[1] != null) {
        //     Astarmanager.getInstance().find(this.gameManager.tanks[1].instance.position, this.gameManager.tanks[0].instance.position, this.gameManager.tanks[1].instance, deltaTime)
        // }

    }

    public drawGrid() {

        if(this.grid!=null){
            const width = this.grid['_grid'].length;
            const height = this.grid['_grid'][0].length;
    
            for (let x = 0; x < width; x++) {
                for (let y = 0; y < height; y++) {
                    // 获取网格单元格的四个角点（世界坐标）
                    const halfCell = gridConfig.cellSize / 2;
                    const center = this.grid.gridToWorld(x, y);
                    const topLeft = new Vec3(center.x - halfCell, 0, center.z + halfCell);
                    const topRight = new Vec3(center.x + halfCell, 0, center.z + halfCell);
                    const bottomLeft = new Vec3(center.x - halfCell, 0, center.z - halfCell);
                    const bottomRight = new Vec3(center.x + halfCell, 0, center.z - halfCell);
                    this.mainCamera?.camera?.geometryRenderer?.addQuad(topLeft, topRight, bottomRight, bottomLeft, Color.GREEN);
                }
            }
        }
    }


}

